/*
 * Created on Tue Mar 17 21:48:19 CET 2009
 */
package se.sweco.ws;

import java.util.Timer;

import se.sweco.ws.TaxServlet;
import se.sweco.ws.impl.DataStoreImpl;
import se.sweco.ws.impl.TrelocityDataTask;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.service.http.HttpService;

public class Activator implements BundleActivator {

	@Override
	public void start(BundleContext context) throws Exception {
		/* Get the HttpService */
		ServiceTracker httpTracker = new ServiceTracker(context, HttpService.class.getName(), null);
		httpTracker.open();
		HttpService httpService = (HttpService)httpTracker.getService();
		
		DataStore storage = new DataStoreImpl(System.getProperty("tinfo.geo.routes"));

		/* Register our servlets on the HttpService */
		httpService.registerServlet(TaxServlet.DEFAULT_PATH, new TaxServlet(storage), null, null);
		httpService.registerServlet(TrafficServlet.DEFAULT_PATH, new TrafficServlet(storage), null, null);
		
		/* Set up timer for data updates */
		Timer timer = new Timer(true);
		timer.schedule(new TrelocityDataTask(storage), 0, TrelocityDataTask.DEFAULT_INTERVAL_MS);
	}
	
	@Override
	public void stop(BundleContext context) throws Exception {
	}
}